//
//  RVideoPlayItemViewController.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/8/27.
//

#import "RVideoPlayItemViewController.h"
#import "RCacheManager.h"
#import "RCacheObj.h"
#import "RVideoCacheItemView.h"

@interface RVideoPlayItemViewController ()

@property(strong,nonatomic)UILabel *titleLb;

@property(nonatomic,strong)MyFlowLayout *flowLayout;

@property(nonatomic,strong)NSArray<RVideoCacheItemView*>* itemArr;

@end

@implementation RVideoPlayItemViewController

- (void)loadView{
    [super loadView];
    
    MyLinearLayout* popView = [[MyLinearLayout alloc] initWithOrientation:(MyOrientation_Vert)];
    popView.backgroundColor = UIColor.whiteColor;
    popView.myHorzMargin = 0;
    popView.myBottom = 0;
    popView.myHeight = SCREEN_HEIGHT - PX(211)- IPHONE_TOP_SAFEINSET;
    [self.view addSubview:popView];
    self.popView = popView;


    MyRelativeLayout* titleLayout = [MyRelativeLayout new];
    titleLayout.padding = UIEdgeInsetsMake(0, 16, 0, 16);
    titleLayout.myHeight = PX(40.0);
    titleLayout.myMargin = 0;
    [popView addSubview:titleLayout];

    
    UILabel* titleLb = [UILabel lableWithFont:FONT_FIT(15) color:COLOR_BLACK_DARK text:@"选集"];
    titleLb.myLeft = 0;
    titleLb.myCenterY = 0;
    [titleLayout addSubview:titleLb];
    _titleLb = titleLb;
    
    UIButton* closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageNamed:@"details_close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeClicked:) forControlEvents:UIControlEventTouchUpInside];
    closeBtn.mySize = CGSizeMake(32, 32);
    closeBtn.myCenterY = 0;
    closeBtn.myRight = 0;
    [titleLayout addSubview:closeBtn];
    
    UIButton* sortBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sortBtn setImage:[UIImage imageNamed:@"details_sort"] forState:UIControlStateNormal];
    [sortBtn addTarget:self action:@selector(sortClicked:) forControlEvents:UIControlEventTouchUpInside];
    sortBtn.mySize = CGSizeMake(32, 32);
    sortBtn.myCenterY = 0;
    sortBtn.rightPos.equalTo(closeBtn.leftPos).offset(10);
    [titleLayout addSubview:sortBtn];

    
    //底部滚动控件
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.myMargin = 0;
    scrollView.weight = 1.0;
    [popView addSubview:scrollView];

    // 流式布局
    MyFlowLayout *flowLayout = [MyFlowLayout flowLayoutWithOrientation:MyOrientation_Vert arrangedCount:3];
    flowLayout.myHeight = MyLayoutSize.wrap;
    flowLayout.subviewVSpace = 10;
    flowLayout.subviewHSpace = 10;
    flowLayout.gravity = MyGravity_Horz_Between;
    flowLayout.padding = UIEdgeInsetsMake(0, 16, 0, 16);
    flowLayout.myMargin = 0;
    [scrollView addSubview:flowLayout];
    _flowLayout = flowLayout;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTVSeries];
    
    if(self.currentIndex && self.currentIndex.col == self.urlGroup.index){
        [self playSelectAtIndex:self.currentIndex.row];
    }
}

- (void)setupTVSeries{

    WeakSelf
    NSMutableArray* arr = [NSMutableArray array];
    for (int i = 0; i < self.urlGroup.urls.count; i++) {

        RVideoUrlObj* urlObj = self.urlGroup.urls[i];
        
        RVideoCacheItemView* item = [RVideoCacheItemView new];
        item.nameLb.text = urlObj.url_info;
        [item.nameLb sizeToFit];
        item.tag = 100+i;
        item.block = ^(NSInteger integer) {
            [weakSelf dramaDeriesSelectAtIndex:integer];
        };
        [self.flowLayout addSubview:item];
        [arr addObject:item];
    }
    self.itemArr = arr;
}

- (void)playSelectAtIndex:(NSInteger)index{
    for (int i = 0; i < self.itemArr.count; i++) {
        self.itemArr[i].selected = index == i;
        if(index == i){
            self.itemArr[i].statusIcon.image = [UIImage imageNamed:@"details_play_state"];
            [self.itemArr[i].statusIcon sizeToFit];
        }else{
            self.itemArr[i].statusIcon.image = NULL;
        }
    }
}

#pragma mark - Navigation

- (void)sortClicked:(UIButton *)sender{
    sender.selected = !sender.selected;
    
    [self.itemArr makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSEnumerator *enumerator = [self.itemArr reverseObjectEnumerator];
    self.itemArr = [enumerator allObjects];
    [self.itemArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.flowLayout addSubview:obj];
    }];
}


- (void)closeClicked:(UIButton *)sender {
    [self dismissActionSheetController:NULL];
}

#pragma mark -- action
- (void)dramaDeriesSelectAtIndex:(NSInteger)index{
    
    if(index >= self.urlGroup.urls.count) return;
    
    RVideoUrlObj* url = self.urlGroup.urls[index];
    
    if(self.selectBlock){
        self.selectBlock(url,[NSIndexPath indexPathForCol:self.urlGroup.index inRow:index]);
    }
    [self dismissActionSheetController:NULL];
}
@end
